from api_post_class import *
from config import *
import time
recentTaskId = 0

def getRecentTaskId():
    return recentTaskId

def createBatchCallTaskTest(dataType):
    authType = 1

    operation = "BatchCalls"
    function = "createTask"
    taskName = str(time.time())
    #workNumbers = "1025,1024"
    workNumbers = "hhhlll2,jjj,8899,1118,1119,1120,1121,1122,1123,1124,1125,1126,1127"
    for i in range(28, 78):
        workNumbers = workNumbers + ",11" + str(i)
    preset_connection_rate = 1.0
    callRate = 1.0
    isset_switch_number = 1
    switch_number_mode = 0
    switch_number_call_mode = 0
    #switch_number_list = "02566684371,02566684374"
    switch_number_list = "02566687671"
    params = {
        "name": taskName,
        "workNumbers": workNumbers,
        "preset_connection_rate": preset_connection_rate,
        "callRate": callRate,
        "isset_switch_number": isset_switch_number,
        "switch_number_mode": switch_number_mode,
        "switch_number_call_mode": switch_number_call_mode,
        "switch_number_list": switch_number_list,
    }
    f = ApiPost(auth_info, domain, version, dataType)

    f.set_func_mode(operation, function, authType)
    rsp_dic = f.post_info_to_api(params)
    print(rsp_dic)
    try:
        global recentTaskId
        if rsp_dic['resp']['respCode'] != 0 and rsp_dic['resp']['respCode'] != '0':
            print("Error, please check and retry")
        recentTaskId = int(rsp_dic['resp']['createTask']['taskId'])
        print(recentTaskId)
        return recentTaskId
    except Exception as e:
        print(e)
        return -1

def startBatchCallTaskTest(dataType):
    authType = 1
    if getRecentTaskId() == 0:
        task_id = createBatchCallTaskTest("json")
        task_id = createBatchCallTaskTest("xml")
    else:
        task_id = getRecentTaskId()

    operation = "BatchCalls"
    function = "startTask"

    params = {
        "taskId": task_id,
    }
    f = ApiPost(auth_info, domain, version, dataType)

    f.set_func_mode(operation, function, authType)
    rsp_dic = f.post_info_to_api(params)
    print(rsp_dic)
    try:
        global recentTaskId
        if rsp_dic['resp']['respCode'] != 0 and rsp_dic['resp']['respCode'] != '0':
            print("Error, please check and retry")
            return -1
        return 0
    except Exception as e:
        print(e)
        return -1